using CampusDataOperationLibrary;
using CampusModel.Model;
using CampusSignInManageSystemWebAPI.Command.Admin;
using CampusSignInManageSystemWebAPI.Responses.@public;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PublicProject.PublicClass.OutPutModels;
using PublicProject.PublicMethods;
using System.Security.Claims;
using CampusSignInManageSystemWebAPI.Responses.Student;

namespace CampusSignInManageSystemWebAPI.Controllers;

[ApiController]
[Authorize(Roles = "Admin")]
[Route("[controller]/[action]")]
public class AdminController(AdminOperation adminOperation) : ControllerBase
{
    [HttpGet]
    public async Task<OutputModel<List<RegisterClass>>> GetClasses()
    {
        var classes = await adminOperation.GetClassesAsync();
        var result = classes.Select(c => new RegisterClass { ClassName = c.ClassName }).ToList();
        return OutPutMethod<List<RegisterClass>>.Success(result, message: "获取成功");
    }

    [HttpPost]
    public async Task<OutputModel<bool>> AddClass([FromBody] ClassCommand command)
    {
        if (string.IsNullOrEmpty(command.ClassName))
        {
            return OutPutMethod<bool>.Failure("班级名称不能为空");
        }

        var result = await adminOperation.AddClassAsync(command.ClassName);
        return OutPutMethod<bool>.Success(result, message: "添加成功");
    }

    [HttpPut]
    public async Task<OutputModel<bool>> UpdateClass([FromBody] UpdateClassCommand command)
    {
        if (string.IsNullOrEmpty(command.OldClassName) || string.IsNullOrEmpty(command.NewClassName))
        {
            return OutPutMethod<bool>.Failure("班级名称不能为空");
        }

        var result = await adminOperation.UpdateClassAsync(command.OldClassName, command.NewClassName);
        return OutPutMethod<bool>.Success(result, message: "更新成功");
    }

    [HttpDelete]
    public async Task<OutputModel<bool>> DeleteClass([FromQuery] string className)
    {
        if (string.IsNullOrEmpty(className))
        {
            return OutPutMethod<bool>.Failure("班级名称不能为空");
        }

        var result = await adminOperation.DeleteClassAsync(className);
        return OutPutMethod<bool>.Success(result, message: "删除成功");
    }
}
